/*
 * Copyright 2011-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sabayframework.cluster.impl;

import java.io.IOException;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.PosixParser;

public class Main {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws Exception {
		Options options = new Options();
		options.addOption("h", false, "print this message");
		options.addOption("d", false, "debug mode output");
		options.addOption("p", true, "network port");
		options.addOption("c", true, "cluster name");
		options.addOption("f", true, "cluster configuration file");
		options.addOption("b", true, "max buffer size,b");
		options.addOption("q", true, "max queue size");
		CommandLineParser parser = new PosixParser();
		CommandLine cmd = parser.parse( options, args);
		if(cmd.hasOption('h')) {
			HelpFormatter formatter = new HelpFormatter();
			formatter.printHelp( "java <java options> "+Main.class.getName(), options );
			System.exit(-1);
		}else{
			ClusteringServiceImpl instance = new ClusteringServiceImpl();
			if(cmd.hasOption('d')){
				org.sabayframework.log.Log.DEBUG();
			}
			if(cmd.hasOption('p')){
				instance.setPort(Integer.parseInt(cmd.getOptionValue('p')));
			}
			if(cmd.hasOption('b')){
				instance.setMaxBufferSize(Integer.parseInt(cmd.getOptionValue('b')));
			}
			if(cmd.hasOption('q')){
				instance.setMaxQueueSize(Integer.parseInt(cmd.getOptionValue('q')));
			}
			if(cmd.hasOption('c')){
				instance.setClusterName(cmd.getOptionValue('c'));
			}
			if(cmd.hasOption('f')){
				instance.setPropertyFile(cmd.getOptionValue('f'));
			}
			try {
				instance.start();
			} catch (Exception e) {
	            System.err.println("Could not start: " + e);
	            e.printStackTrace();
	            System.exit(-1);
			}
		}
	}

}
